package Presentacion.notas;

import static Presentacion.academico.frmConsultaPeriodo.generartabla;
import clases.academico.MateriaPeriodoCurso;
import clases.academico.PeriodoCurso;
import clases.notas.Notas;
import clases.notas.Parcial;
import clases.notas.Quimestre;
import javax.swing.table.DefaultTableModel;

public class frmnotas extends javax.swing.JInternalFrame {

    private static int pagina;
    private static String sql;
    private static Notas objnota;

    /**
     * Creates new form frmnotas
     */
    public frmnotas() {
        initComponents();
        pagina = 1;
        objnota = new Notas();
        sql = objnota.SqlGeneralNotas();
        generartabla();
        inicializar();
    }

    private void inicializar() {
        txtalumno.setText("");
        txtalumno.setVisible(false);
        cbocurso.setVisible(false);
        cbomateria.setVisible(false);
        cboquimistre.setVisible(false);
        cboparcial.setVisible(false);
        // objperiodoCurso.periodo.listarPeriodoCombo(cboperiodo);
    }

    public static void generartabla() {
        objnota.ConsultaNotas(new DefaultTableModel(), tblnotas,
                sql, lbltotalpaginas, lblpagina, pagina);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbocriterio = new javax.swing.JComboBox();
        txtalumno = new javax.swing.JTextField();
        cbocurso = new javax.swing.JComboBox();
        cbomateria = new javax.swing.JComboBox();
        cboquimistre = new javax.swing.JComboBox();
        cboparcial = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblnotas = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnprimero = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel4 = new javax.swing.JLabel();
        lblpagina = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbltotalpaginas = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnsiguiente = new javax.swing.JButton();
        btnultimo = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSQUEDA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("CRITERIO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, -1));

        cbocriterio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODO", "CURSO", "MATERIA", "ALUMNO", "QUIMISTRE", "PARCIAL" }));
        cbocriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocriterioActionPerformed(evt);
            }
        });
        jPanel1.add(cbocriterio, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 120, -1));

        txtalumno.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtalumnoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtalumnoKeyTyped(evt);
            }
        });
        jPanel1.add(txtalumno, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 370, -1));

        cbocurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocursoActionPerformed(evt);
            }
        });
        jPanel1.add(cbocurso, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 370, -1));

        cbomateria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbomateriaActionPerformed(evt);
            }
        });
        jPanel1.add(cbomateria, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 250, -1));

        cboquimistre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboquimistreActionPerformed(evt);
            }
        });
        jPanel1.add(cboquimistre, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 180, -1));

        cboparcial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboparcialActionPerformed(evt);
            }
        });
        jPanel1.add(cboparcial, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 160, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblnotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblnotas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblnotasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblnotas);

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnprimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/primer.png"))); // NOI18N
        btnprimero.setFocusable(false);
        btnprimero.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnprimero.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnprimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnprimeroActionPerformed(evt);
            }
        });
        jToolBar1.add(btnprimero);

        btnatras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/atras.png"))); // NOI18N
        btnatras.setToolTipText("");
        btnatras.setFocusable(false);
        btnatras.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnatras.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        jToolBar1.add(btnatras);
        jToolBar1.add(jSeparator1);

        jLabel4.setText("Pagina ");
        jToolBar1.add(jLabel4);

        lblpagina.setText("jLabel2");
        jToolBar1.add(lblpagina);

        jLabel5.setText(" de ");
        jToolBar1.add(jLabel5);

        lbltotalpaginas.setText("jLabel1");
        jToolBar1.add(lbltotalpaginas);
        jToolBar1.add(jSeparator2);

        btnsiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/siguiente.png"))); // NOI18N
        btnsiguiente.setFocusable(false);
        btnsiguiente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsiguiente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnsiguiente);

        btnultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ultimos.png"))); // NOI18N
        btnultimo.setFocusable(false);
        btnultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnultimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnultimo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbocriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocriterioActionPerformed
        inicializar();
        if (cbocriterio.getSelectedItem() == "TODO") {
            pagina = 1;
            sql = objnota.SqlGeneralNotas();
            generartabla();
        } else if (cbocriterio.getSelectedItem() == "ALUMNO") {
            txtalumno.setVisible(true);
            txtalumno.requestFocus();

        } else if (cbocriterio.getSelectedItem() == "CURSO") {
            cbocurso.setVisible(true);
            cbocurso.requestFocus();
            PeriodoCurso curso = new PeriodoCurso();
            curso.listarCursoActivoCombo(cbocurso);
        } else if (cbocriterio.getSelectedItem() == "MATERIA") {
            cbomateria.setVisible(true);
            cbomateria.requestFocus();
            MateriaPeriodoCurso materia=new MateriaPeriodoCurso();
            materia.listarMateriaActivaCombo(cbomateria);
        } else if (cbocriterio.getSelectedItem() == "QUIMISTRE") {
            cboquimistre.setVisible(true);
            cboquimistre.requestFocus();
            Quimestre q = new Quimestre();
            q.llenarQuimestre(cboquimistre);
        } else if (cbocriterio.getSelectedItem() == "PARCIAL") {
            cboparcial.setVisible(true);
            cboparcial.requestFocus();
            Parcial p = new Parcial();
            p.llenarParcial(cboparcial);
        }
    }//GEN-LAST:event_cbocriterioActionPerformed

    private void txtalumnoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtalumnoKeyReleased
        global.Validaciones.convertirMayuscula(txtalumno);
        if ((global.Validaciones.comprobarNulo(txtalumno))) {
            pagina = 1;
            sql = objnota.SqlNotasAlumno(txtalumno.getText());
            generartabla();
        }
    }//GEN-LAST:event_txtalumnoKeyReleased

    private void txtalumnoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtalumnoKeyTyped
        global.Validaciones.numerosLetras(evt, txtalumno);
    }//GEN-LAST:event_txtalumnoKeyTyped

    private void cbocursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocursoActionPerformed
        if (cbocurso.getSelectedIndex() != 0 && cbocurso.getSelectedIndex() != -1) {
            pagina = 1;
            sql = objnota.SqlNotasCurso(cbocurso.getSelectedItem().toString());
            generartabla();
        }
    }//GEN-LAST:event_cbocursoActionPerformed

    private void cbomateriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbomateriaActionPerformed
        if (cbomateria.getSelectedIndex() != 0) {
            pagina = 1;
            sql = objnota.SqlNotasMateria(cbomateria.getSelectedItem().toString());
            generartabla();
        }
    }//GEN-LAST:event_cbomateriaActionPerformed

    private void tblnotasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblnotasMouseClicked
        int click = evt.getClickCount();
        if (click == 2) {
            // llenarDialogo(tblnotas.getSelectedRow());

        }
    }//GEN-LAST:event_tblnotasMouseClicked

    private void btnprimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnprimeroActionPerformed
        pagina = 1;
        generartabla();
    }//GEN-LAST:event_btnprimeroActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        if (pagina > 1) {
            pagina--;
            generartabla();
        }
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
        if (objnota.objPaginacion.getTotalpaginas() > pagina) {
            pagina++;
            generartabla();
        }
    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btnultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnultimoActionPerformed
        pagina = objnota.objPaginacion.getTotalpaginas();
        generartabla();
    }//GEN-LAST:event_btnultimoActionPerformed

    private void cboquimistreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboquimistreActionPerformed
        if (cboquimistre.getSelectedIndex() != 0 && cboquimistre.getSelectedIndex() != -1) {
            pagina = 1;
            sql = objnota.SqlNotasQuimistre(cboquimistre.getSelectedItem().toString());
            generartabla();
        }
    }//GEN-LAST:event_cboquimistreActionPerformed

    private void cboparcialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboparcialActionPerformed
        if (cboparcial.getSelectedIndex() != 0 && cboparcial.getSelectedIndex() != -1) {
            pagina = 1;
            sql = objnota.SqlNotasParcial(cboparcial.getSelectedItem().toString());
            generartabla();
        }
    }//GEN-LAST:event_cboparcialActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnprimero;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JButton btnultimo;
    private javax.swing.JComboBox cbocriterio;
    private javax.swing.JComboBox cbocurso;
    private javax.swing.JComboBox cbomateria;
    private javax.swing.JComboBox cboparcial;
    private javax.swing.JComboBox cboquimistre;
    private javax.swing.JLabel jLabel3;
    private static javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private static javax.swing.JLabel lblpagina;
    private static javax.swing.JLabel lbltotalpaginas;
    public static javax.swing.JTable tblnotas;
    private javax.swing.JTextField txtalumno;
    // End of variables declaration//GEN-END:variables
}
